package com.zbcz.erp.util

import android.app.DatePickerDialog
import android.content.Context
import android.view.Gravity
import android.view.WindowManager
import com.zbcz.erp.R
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
import java.util.Calendar
import java.util.Date
import java.util.Locale

const val DATE_FORMAT = "yyyy年MM月dd日"
const val UPLOAD_FORMAT = "yyyy-MM-dd"
const val UPLOAD_FORMAT_WHOLE = "yyyy-MM-dd HH:mm:s"
fun formatDate(
    year: Int,
    month: Int,
    dayOfMonth: Int,
    formatPattern: String = DATE_FORMAT
): String? {
    val calendar = Calendar.getInstance()
    calendar[year, month] = dayOfMonth
    val sdf = SimpleDateFormat(formatPattern, Locale.getDefault())
    return sdf.format(calendar.time)
}

fun getCurrentDateFormatted(formatPattern: String = DATE_FORMAT): String? {
    val calendar = Calendar.getInstance()
    val sdf = SimpleDateFormat(formatPattern, Locale.getDefault())
    return sdf.format(calendar.time)
}

fun showDatePickerDialog(
    context: Context,
    onDateSelected: (year: Int, month: Int, dayOfMonth: Int) -> Unit,
    initialYear: Int = -1, // 添加初始年份参数，默认为-1表示使用当前年份
    initialMonth: Int = -1, // 添加初始月份参数，默认为-1表示使用当前月份
    initialDayOfMonth: Int = -1 // 添加初始日期参数，默认为-1表示使用当前日期
) {
    // 获取当前日期
    val calendar: Calendar = Calendar.getInstance()
    val year: Int = if (initialYear != -1) initialYear else calendar.get(Calendar.YEAR)
    val month: Int = if (initialMonth != -1) initialMonth else calendar.get(Calendar.MONTH)
    val dayOfMonth: Int =
        if (initialDayOfMonth != -1) initialDayOfMonth else calendar.get(Calendar.DAY_OF_MONTH)

    // 创建日期选择Dialog，并设置默认选中的日期为当前日期
    val datePickerDialog = DatePickerDialog(
        context, R.style.CustomDatePickerDialogTheme,
        { _, selectedYear, selectedMonth, selectedDayOfMonth -> // 处理日期选择结果
            onDateSelected(selectedYear, selectedMonth, selectedDayOfMonth)
        }, year, month, dayOfMonth
    )

    // 设置日期选择Dialog的样式为日历样式
    datePickerDialog.datePicker.calendarViewShown = true
    datePickerDialog.datePicker.spinnersShown = false // 隐藏下拉式的年份和月份选择器

    // 居中对话框
    val window = datePickerDialog.window
    window?.setGravity(Gravity.CENTER)
    val layoutParams = WindowManager.LayoutParams()
    layoutParams.copyFrom(window?.attributes)
    layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
    layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
    window?.attributes = layoutParams

    // 显示日期选择Dialog
    datePickerDialog.show()
}

fun showDatePickerDialog(
    context: Context,
    onDateSelected: (year: Int, month: Int, dayOfMonth: Int) -> Unit,
) {
    // 获取当前日期
    val calendar: Calendar = Calendar.getInstance()
    val year: Int = calendar.get(Calendar.YEAR)
    val month: Int = calendar.get(Calendar.MONTH)
    val dayOfMonth: Int = calendar.get(Calendar.DAY_OF_MONTH)

    // 创建日期选择Dialog，并设置默认选中的日期为当前日期
    val datePickerDialog = DatePickerDialog(
        context, R.style.CustomDatePickerDialogTheme,
        { _, year, month, dayOfMonth -> // 处理日期选择结果
            onDateSelected(year, month, dayOfMonth)
        }, year, month, dayOfMonth
    )

    // 设置日期选择Dialog的样式为日历样式
    datePickerDialog.datePicker.calendarViewShown = true
    datePickerDialog.datePicker.spinnersShown = false // 隐藏下拉式的年份和月份选择器

    // 居中对话框
    val window = datePickerDialog.window
    window?.setGravity(Gravity.CENTER)
    val layoutParams = WindowManager.LayoutParams()
    layoutParams.copyFrom(window?.attributes)
    layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
    layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
    window?.attributes = layoutParams

    // 显示日期选择Dialog
    datePickerDialog.show()
}

fun compareDates(date1: String?, date2: String?, formatPattern: String = DATE_FORMAT): Int {
    val sdf = SimpleDateFormat(formatPattern, Locale.getDefault())
    val parsedDate1: Date
    val parsedDate2: Date

    try {
        parsedDate1 = sdf.parse(date1) ?: return 0
        parsedDate2 = sdf.parse(date2) ?: return 0
    } catch (e: Exception) {
        e.printStackTrace()
        return 0
    }

    return parsedDate1.compareTo(parsedDate2)
}

fun formatDateTime(dateTimeString: String): String {
    return try {
        val inputFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        val outputFormat = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
        val date = inputFormat.parse(dateTimeString)
        outputFormat.format(date)
    } catch (e: Exception) {
        e.printStackTrace()
        ""
    }
}

fun convertDateFormat(dateStr: String): String {
    return try {
        // 定义输入和输出的日期格式
        val inputFormat = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
        val outputFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())

        // 解析输入的日期字符串
        val date = inputFormat.parse(dateStr)

        // 格式化为目标日期格式
        outputFormat.format(date)
    } catch (e: ParseException) {
        // 处理解析失败的异常
        println("日期格式错误: $dateStr")
        "" // 返回空字符串或其他合适的默认值
    }
}